const baseUrl = "https://jsonplaceholder.typicode.com";

let isRefreshing = false;
let refreshSubscribers : ((token : string) => void)[] = [];

// 模拟存储 token
let accessToken = "your-access-token";
let refreshToken = "your-refresh-token";

const header = {
	"Content-Type": "application/json",
};

async function request(
	url : string,
	method : 'GET' | 'POST' = 'GET',
	data : string | object | ArrayBuffer = {}
) {
	// try {
	// 	uni.showLoading({ title: '加载中...' });

		const response = await new Promise((resolve, reject) => {
			uni.request({
				url: baseUrl + url,
				method,
				data,
				header: {
					...header,
					Authorization: `Bearer ${accessToken}`,
				},
				success: (res) => {
					uni.hideLoading();
					if (res.statusCode === 200) {
						resolve(res.data);
					} else if (res.statusCode === 401) {
						uni.showToast({
							title: "身份过期，请重新登录",
							icon: "none",
							duration: 1000
						});
						resolve(res)
						// 拦截 401 错误，尝试刷新 token
						// handleTokenRefresh(() => {
						//   // 重新设置新的 token 后再次发起请求
						//   res.config.header.Authorization = `Bearer ${accessToken}`;
						//   uni.request(res.config);
						// });
						// } else {
						//   handleStatusCode(res);
						//   resolve(res);
					} else if (res.statusCode === 500 || res.statusCode == 200) {
						uni.showToast({
							title: "服务器错误",
							icon: "none",
							duration: 1000
						});
						resolve(res)
					} else {
						uni.showToast({
							title: "未知错误",
							icon: "none",
							duration: 1000
						})
						resolve(res)
					}
				},
				fail: (err) => {
					uni.hideLoading();
					uni.showToast({ title: "服务器错误", icon: "none" });
					reject(err);
				},
			});
		});

		return response;
	// } catch (error) {
	// 	uni.showToast({ title: "网络异常", icon: "none" });
	// 	throw error;
	// }
}
//     // 发起刷新 token 请求
// function handleTokenRefresh(cb: () => void) {
//   if (!isRefreshing) {
//     isRefreshing = true;
//     uni.request({
//       url: baseUrl + "/refresh-token",
//       method: "POST",
//       data: { refreshToken },
//       success: (res) => {
//         if (res.statusCode === 200) {
//           accessToken = res.data.accessToken;
//           refreshToken = res.data.refreshToken;

//           isRefreshing = false;
//           refreshSubscribers.forEach((subscriber) => subscriber(accessToken));
//           refreshSubscribers = [];
//           cb();
//         } else {
//           uni.showToast({ title: "登录已过期", icon: "none" });
//           uni.navigateTo({ url: "/pages/login/login" });
//         }
//       },
//       fail: () => {
//         uni.showToast({ title: "刷新 token 失败", icon: "none" });
//         uni.navigateTo({ url: "/pages/login/login" });
//       },
//     });
//   } else {
//     // 等待刷新完成后再执行请求
//     refreshSubscribers.push(cb);
//   }
// }

// function handleStatusCode(res: any) {
//   switch (res.statusCode) {
//     case 401:
//       uni.showToast({ title: "信息过期", icon: "none" });
//       break;
//     case 500:
//     case 202:
//       uni.showToast({ title: "服务器错误", icon: "none" });
//       break;
//     default:
//       uni.showToast({ title: "未知错误", icon: "none" });
//   }
// }

export default request;